<?php
require_once WEB_MODEL_DIR . DIRECTORY_SEPARATOR . 'DAO' . DIRECTORY_SEPARATOR . 'AppDAO.php';
require_once WEB_MODEL_DIR . DIRECTORY_SEPARATOR . 'DAO' . DIRECTORY_SEPARATOR . 'AppLinkDAO.php';
require_once WEB_MODEL_DIR . DIRECTORY_SEPARATOR . 'DAO' . DIRECTORY_SEPARATOR . 'AppMessageDAO.php';
class AppService {

    public function getAllApp($context, $result, $start, $limit) {
        $appArr = null;
        try {
            $appDAO = new AppDAO($context);

            $appArr = $appDAO -> getAllApp($start, $limit);
            if (isset($appArr) && !empty($appArr)) {
                $result -> setSuccess();
            } else {
                $result -> setFailed("ERR01", "No App Found");
                $context -> logError("ERR01", "No App Found Call By " . __METHOD__ . " ON " . __CLASS__);
            }
        } catch (Exception $e) {
            $result -> setException($e);
            $context -> logException($e);
        }

        return $appArr;
    }
    
	public function getApp($context, $result) {
        $appArr = null;
        try {
            $appDAO = new AppDAO($context);

            $appArr = $appDAO -> getApp();
            if (isset($appArr) && !empty($appArr)) {
                $result -> setSuccess();
            } else {
                $result -> setFailed("ERR01", "No App Found");
                $context -> logError("ERR01", "No App Found Call By " . __METHOD__ . " ON " . __CLASS__);
            }
        } catch (Exception $e) {
            $result -> setException($e);
            $context -> logException($e);
        }

        return $appArr;
    }
    
	public function countApp($context, $result) {
        $total = 0;
        try {
            $appDAO = new AppDAO($context);

            $total = $appDAO -> countApp();
            
        } catch (Exception $e) {
            $result -> setException($e);
            $context -> logException($e);
        }

        return $total;
    }

    public function getAppByStatus($context, $result, $status, $start, $limit) {
        $appArr = null;
        try {
            $appDAO = new AppDAO($context);

            $appArr = $appDAO -> getAppByStatus($status, $start, $limit);
            if (isset($appArr) && !empty($appArr)) {
                $result -> setSuccess();
            } else {
                $result -> setFailed("ERR01", "No App Found");
                $context -> logError("ERR01", "No App Found Call By " . __METHOD__ . " ON " . __CLASS__);
            }
        } catch (Exception $e) {
            $result -> setException($e);
            $context -> logException($e);
        }

        return $appArr;
    }
    
	public function getAppNameByStatus($context, $result, $status) {
        $appArr = null;
        try {
            $appDAO = new AppDAO($context);

            $appArr = $appDAO -> getAppNameByStatus($status);
            if (isset($appArr) && !empty($appArr)) {
                $result -> setSuccess();
            } else {
                $result -> setFailed("ERR01", "No App Found");
                $context -> logError("ERR01", "No App Found Call By " . __METHOD__ . " ON " . __CLASS__);
            }
        } catch (Exception $e) {
            $result -> setException($e);
            $context -> logException($e);
        }

        return $appArr;
    }

    public function countAppByStatus($context, $result, $status) {
        $total = 0;
        try {
            $appDAO = new AppDAO($context);

            $total = $appDAO -> countAppByStatus($status);
            
        } catch (Exception $e) {
            $result -> setException($e);
            $context -> logException($e);
        }

        return $total;
    }

    public function getAppById($context, $result, $id) {
        $appArr = null;
        try {
            $appDAO = new AppDAO($context);

            $appArr = $appDAO -> getAppById($id);
            if (isset($appArr) && !empty($appArr)) {
                $result -> setSuccess();
            } else {
                $result -> setFailed("ERR01", "No App Found By Id : " . $id);
                $context -> logError("ERR01", "No App Found By Id : " . $id . " Call By " . __METHOD__ . " ON " . __CLASS__);
            }
        } catch (Exception $e) {
            $result -> setException($e);
            $context -> logException($e);
        }

        return $appArr;
    }
    
	public function getAllAppByName($context, $result, $name) {
        $appArr = null;
        try {
            $appDAO = new AppDAO($context);

            $appArr = $appDAO -> getAllAppByName($name);
            if (isset($appArr) && !empty($appArr)) {
                $result -> setSuccess();
            } else {
                $result -> setFailed("ERR01", "No App Found By Id : " . $id);
                $context -> logError("ERR01", "No App Found By Id : " . $id . " Call By " . __METHOD__ . " ON " . __CLASS__);
            }
        } catch (Exception $e) {
            $result -> setException($e);
            $context -> logException($e);
        }

        return $appArr;
    }

    public function getAppLinkByAppId($context, $result, $id) {
        $resultAppLink = null;
        try {
            $appLinkDAO = new AppLinkDAO($context);

            $appLinkArr = $appLinkDAO -> getAppLinkByAppId($id);
            if (isset($appLinkArr) && !empty($appLinkArr)) {
                $resultAppLink = array();
                foreach($appLinkArr as $appLink)
                {
                    $appType = $appLink['type'];
                    $appDeviceId = $appLink['device_id'];
                    $fileType = $appLink['file_type'];
                    $deviceName = !empty($appLink['device_name']) ? $appLink['device_name'] : 'Tất cả các dòng máy ' . $appType;

                    $appDeviceArr = array();
                    $resultAppLink[$appType][$appDeviceId]['device_name'] = $deviceName;
                    $resultAppLink[$appType][$appDeviceId]['appLinkArr'][$fileType] = $appLink;
                }
                $result -> setSuccess();
            } else {
                $result -> setFailed("ERR01", "No App Link Found");
                $context -> logError("ERR01", "No App Link Found By " . __METHOD__ . " ON " . __CLASS__);
            }
        } catch (Exception $e) {
            $result -> setException($e);
            $context -> logException($e);
        }

        return $resultAppLink;
    }

    public function getAppMessageByAppId($context, $result, $id) {
        $appMessage = null;
        try {
            $appDAO = new AppMessageDAO($context);

            $appMessage = $appDAO -> getAppMessageByAppId($id);
            if (isset($appMessage) && !empty($appMessage)) {
                $result -> setSuccess();
            } else {
                $result -> setFailed("ERR01", "No App Found By Id : " . $id);
                $context -> logError("ERR01", "No App Found By Id : " . $id . " Call By " . __METHOD__ . " ON " . __CLASS__);
            }
        } catch (Exception $e) {
            $result -> setException($e);
            $context -> logException($e);
        }

        return $appMessage;
    }
    
	public function insertApp($context, $result,$param) {
        $appArr = null;
        try {
            $gameLinkDAO = new AppDAO($context);

            $appArr = $gameLinkDAO -> insertApp($param);
        } catch (Exception $e) {
            $result -> setException($e);
            $context -> logException($e);
        }
        return $appArr;
    }
    
	public function updateApp($context, $result,$param) {
        $appArr = null;
        try {
            $gameLinkDAO = new AppDAO($context);

            $appArr = $gameLinkDAO -> updateApp($param);
        } catch (Exception $e) {
            $result -> setException($e);
            $context -> logException($e);
        }
        return $appArr;
    }
    
	public function deleteApp($context, $result, $id) {
        try {

            $gameLinkDAO = new AppDAO($context);

            $gameLinkDAO -> deleteApp($id);
        } catch (Exception $e) {
            $result -> setException($e);
            $context -> logException($e);
        }
    }
}
?>